//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    foods: ["野葱炒牛肉", "萝卜炖牛肉", "狗肉火锅", "羊肉萝卜汤", "香菜羊肉水饺", "小炒河虾", "梅干菜蒸鸭", "京酱肉丝", "徽式炒面", "吃个屁"],
    currentFood:'点几次会吃到屁呢？',
    timer:null
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  startRandom: function(e) {
    if(this.data.timer) return false;
    let _this = this;
    let len = this.data.foods.length;
    let n;
    this.data.timer = setInterval(()=>{
      n = Math.floor(Math.random() * len);
      this.setData({
        currentFood : this.data.foods[n]
      })
    },30);
  },
  stopRandom:function(e){
    clearInterval(this.data.timer);
    this.setData({
      timer:null
    })
  }
})
